package com.swas.service.impl;

import com.swas.dao.PatrolCheckDao;
import com.swas.dao.PatrolCheckMapper;
import com.swas.dao.PatrolTaskMapper;
import com.swas.entity.PatrolCheck;
import com.swas.entity.PatrolCheckExample;
import com.swas.entity.PatrolTask;
import com.swas.service.PatrolCheckService;
import com.swas.vo.CheckTableVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class PatrolCheckServiceImpl implements PatrolCheckService {
    @Autowired
    private PatrolCheckMapper patrolCheckMapper;

    @Autowired
    private PatrolCheckDao patrolCheckDao;

    @Autowired
    private PatrolTaskMapper patrolTaskMapper;

    @Override
    public List<CheckTableVO> findAll(Integer pageNum, Integer pageSize,String taskno,String taskname,String tasktype,Integer taskstatu) {
        List<CheckTableVO> checkTableVOS = patrolCheckMapper.selectAll(taskno,taskname,tasktype,taskstatu);
        return checkTableVOS;
    }

    @Override
    public boolean updateCheck(CheckTableVO checkTableVO) {
        PatrolTask patrolTask = new PatrolTask();
        patrolTask.setId(checkTableVO.getId());
        patrolTask.setTaskstatu(checkTableVO.getTaskstatu());
        int i = patrolTaskMapper.updateByPrimaryKeySelective(patrolTask);

        PatrolCheckExample patrolCheckExample = new PatrolCheckExample();
        PatrolCheckExample.Criteria criteria = patrolCheckExample.createCriteria();
        criteria.andTaskidEqualTo(checkTableVO.getId());
        patrolCheckDao.deleteByExample(patrolCheckExample);

        PatrolCheck patrolCheck = new PatrolCheck();
        patrolCheck.setCkdate(new Date());
        System.out.println(new Date());
        patrolCheck.setCkresult(checkTableVO.getTaskstatu().equals(3)?"审核通过":"审核不通过");
        patrolCheck.setTaskid(checkTableVO.getId());
        patrolCheckMapper.insertSelective(patrolCheck);
        return i>0?true:false;
    }

    @Override
    public PatrolCheck getByTaskId(Integer taskid) {
        PatrolCheckExample example = new PatrolCheckExample();
        example.createCriteria().andTaskidEqualTo(taskid);
        List<PatrolCheck> patrolChecks = patrolCheckDao.selectByExample(example);
        if (patrolChecks.size() > 0) {
            return patrolChecks.get(0);
        }
        return new PatrolCheck();
    }
}
